package algorithm;

import it.unisa.dia.gas.jpbc.Element;
import it.unisa.dia.gas.jpbc.Field;
import it.unisa.dia.gas.jpbc.Pairing;

import java.math.BigInteger;
import java.util.Arrays;
import java.util.Base64;

public class CPABEAttKey {
    private Element D;
    private EAUnion[] Dj1;
    private EAUnion[] Dj2;
    private String[] attlist;


    public CPABEAttKey(Element d, EAUnion[] dj1, EAUnion[] Dj2, String[] attList) {
        D = d;
        Dj1 = dj1;
        this.Dj2 = Dj2;
        this.attlist=attList;
    }

    public CPABEAttKey() {
    }

    public Element getD() {
        return D;
    }

    public void setD(Element d) {
        D = d;
    }

    public EAUnion[] getDj1() {
        return Dj1;
    }

    public EAUnion[] getDj2() {
        return Dj2;
    }


    public String[] getAttlist() {
        return attlist;
    }

    public String[] toStringArray(){
        String[] array=new String[4];
        array[0]= Base64.getEncoder().encodeToString(D.toBytes());
        StringBuilder builder1=new StringBuilder();
        for (int i = 0; i < Dj1.length; i++) {

            builder1.append(Dj1[i].toString());
            //数组界限分隔符
            builder1.append("%");
        }
        array[1]=builder1.toString();
        StringBuilder builder2=new StringBuilder();
        for (int i = 0; i < Dj2.length; i++) {
            builder2.append(Dj2[i].toString());
            //数组界限分隔符
            builder2.append("%");
        }
        array[2]=builder2.toString();
        StringBuilder builder3=new StringBuilder();
        for (int i = 0; i < attlist.length; i++) {
            builder3.append(attlist[i]);
            builder3.append("@");
        }
        array[3]=builder3.toString();
        return array;
    }

    public void fromStringArray(String[] input,Pairing pairing){
        Field G1=pairing.getG1();
        D=G1.newElement();
        D.setFromBytes(Base64.getDecoder().decode(input[0]));
        D=D.getImmutable();
        String[] forDj1=input[1].split("%");
        Dj1=new EAUnion[forDj1.length];
        for (int i = 0; i < forDj1.length; i++) {
            Dj1[i]=new EAUnion();
            Dj1[i].fromString(forDj1[i],G1);
        }
        String[] forDj2=input[2].split("%");
        Dj2=new EAUnion[forDj2.length];
        for (int i = 0; i < forDj2.length; i++) {
            Dj2[i]=new EAUnion();
            Dj2[i].fromString(forDj2[i],G1);
        }
        attlist=input[3].split("@");
    }
}
